package com.example.dell.fastshopping.user.address;

import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;
import android.content.Context;
import android.content.res.AssetManager;
import android.util.Log;

import com.example.dell.fastshopping.MainActivity;
import com.example.dell.fastshopping.network.ApiService;
import com.example.dell.fastshopping.network.NetCallback;
import com.example.dell.fastshopping.network.RetrofitClient;
import com.example.dell.fastshopping.network.http.BaseResponse;
import com.example.dell.fastshopping.utils.KLog;
import com.example.dell.fastshopping.utils.ToastUtils;
import com.google.gson.GsonBuilder;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.http.Query;

public class AddressViewMolder extends ViewModel {
    private ApiService service = RetrofitClient.getInstance().create(ApiService.class);
    private MutableLiveData<List<MyAddress>> addressList = new MutableLiveData<>();
    private MutableLiveData<BaseResponse> deleteAddress = new MutableLiveData<>();
    private MutableLiveData<BaseResponse> editAddress = new MutableLiveData<>();
    private MutableLiveData<BaseResponse> addAddress = new MutableLiveData<>();

    public MutableLiveData<List<MyAddress>> getAddressList() {
        RetrofitClient.execute(service.getAddressList(), new NetCallback<BaseResponse<List<MyAddress>>>() {
            @Override
            public void netCallback(BaseResponse<List<MyAddress>> baseResponse) {
                if (baseResponse.isSuccess())
                    addressList.setValue(baseResponse.getData());
            }
        });
        return addressList;
    }

    public MutableLiveData<BaseResponse> deleteAddress(MyAddress address) {
        String s = new GsonBuilder().create().toJson(address);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), s);

        RetrofitClient.execute(service.deleteAddress(body), new NetCallback<BaseResponse>() {
            @Override
            public void netCallback(BaseResponse response) {
                deleteAddress.setValue(response);
            }
        });
        return deleteAddress;
    }


    public MutableLiveData<BaseResponse> editAddress(MyAddress myAddress) {
        String s = new GsonBuilder().create().toJson(myAddress);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), s);

        RetrofitClient.execute(service.editAddress(body), new NetCallback<BaseResponse>() {
            @Override
            public void netCallback(BaseResponse response) {
                editAddress.setValue(response);
            }
        });
        return editAddress;
    }

    public MutableLiveData<BaseResponse> addAddress(String name, String area, String phone, String address, int status) {

//        @Query("userName") String userName, @Query("area") String area, @Query("phone") String phone,
//        @Query("address") String address, @Query("defaults") int status

        HashMap<String, Object> map = new HashMap<>();
        map.put("userName", name);
        map.put("area", area);
        map.put("phone", phone);
        map.put("address", address);
        map.put("defaults", status);

        String json = new GsonBuilder().create().toJson(map);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);

        RetrofitClient.execute(service.addAddress(body), new NetCallback<BaseResponse>() {
            @Override
            public void netCallback(BaseResponse response) {
                if (response.isSuccess()) {
                    addAddress.setValue(response);
                }
            }
        });
        return addAddress;
    }

    public AddressEntity getAddress(Context context) {
        String s = null;
        try {
            AssetManager assetManager = context.getAssets();
            InputStream is = assetManager.open("address.json");

            try {
                //格式转换
                Scanner scanner = new Scanner(is, "UTF-8").useDelimiter("\\A");
                if (scanner.hasNext()) {
                    s = scanner.next();
                }
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
                ToastUtils.showShort("地址获取失败");
            }
        } catch (IOException e) {
            ToastUtils.showShort("地址获取失败");
            e.printStackTrace();
        }

        AddressEntity addressEntity = new GsonBuilder().create().fromJson(s, AddressEntity.class);
        return addressEntity;
    }
}
